package com.example.malluser.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mallcommon.RestResult;
import com.example.malluser.api.service.MallUserService;
import com.example.malluser.entity.MallUserEntity;
import com.example.tools.logging.annotation.SysLog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员
 *
 * @author yang
 * @date 2021-08-22 08:16:59
 */
@ApiOperation("用户控制器")
@RestController
@AllArgsConstructor
@RequestMapping("/malluser")
public class MallUserController {

  @Autowired
  private final MallUserService mallUserService;

  /**
   * 分页查询
   *
   * @param page 分页对象
   * @param mallUser 管理员
   * @return
   */
  @ApiOperation("分页查询")
  @RequestMapping(value = "/page", method = RequestMethod.GET)
  public RestResult<IPage<MallUserEntity>> getMallUserPage(Page page, MallUserEntity mallUser) {
    return new RestResult<>(mallUserService.page(page, Wrappers.query(mallUser)));
  }

  /**
   * id查询管理员
   *
   * @param uid id
   * @return RestResult
   */
  @SysLog("通过id查询管理员")
  @ApiOperation("通过id查询管理员")
  @ApiImplicitParam(name = "uid", value = "管理员编码", required = true, paramType = "path")
  @RequestMapping(value = "/{uid}", method = RequestMethod.GET)
  public RestResult<MallUserEntity> getById(@PathVariable("uid") Integer uid) {
    return new RestResult<>(mallUserService.getById(uid));
  }

  /**
   * 新增管理员
   *
   * @param mallUserEntity 管理员
   * @return RestResult
   */
  @ApiOperation("新增管理员")
  @ApiImplicitParam(
      name = "mallUserEntity",
      value = "管理员实体类",
      required = true,
      paramType = "body",
      dataType = "MallUserEntity")
  @RequestMapping(method = RequestMethod.POST)
  public RestResult save(@RequestBody MallUserEntity mallUserEntity) {
    return new RestResult<>(mallUserService.save(mallUserEntity));
  }

  /**
   * 修改管理员
   *
   * @param mallUserEntity 管理员
   * @return RestResult
   */
  @ApiOperation("修改管理员")
  @RequestMapping(method = RequestMethod.PUT)
  @ApiImplicitParam(
      name = "mallUserEntity",
      value = "管理员实体类",
      required = true,
      paramType = "body",
      dataType = "MallUserEntity")
  public RestResult updateById(@RequestBody MallUserEntity mallUserEntity) {
    return new RestResult<>(mallUserService.updateById(mallUserEntity));
  }

  /**
   * 通过id删除管理员
   *
   * @param uid id
   * @return RestResultR
   */
  @ApiOperation("删除管理员")
  @ApiImplicitParam(name = "uid", value = "管理员编码", required = true, paramType = "path")
  @RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
  public RestResult removeById(@PathVariable Integer uid) {
    return new RestResult<>(mallUserService.removeById(uid));
  }

  /**
   * 查询全部
   *
   * @param
   * @return RestResult
   */
  @ApiOperation("查询全部")
  @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
  public RestResult<List<MallUserEntity>> queryAll() {
    return new RestResult<>(mallUserService.list());
  }
}
